﻿using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using TrackSystem.Entities.Extensions;
using TrackSystem.Entities.Models;

namespace TrackSystem.Entities.Dtos.Slave;

public class SlaveDto
{
            /// <summary>
        /// 统一标识符
        /// </summary>
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Display(Name = "统一标识符")]
        public int Id { get; set; }
        /// <summary>
        /// 通道名称
        /// </summary>
        [Required]
        [Display(Name = "服务器名称")]
        public required string Name { get; set; }
        /// <summary>
        /// 通道描述
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// 协议类型
        /// </summary>
        public SlaveProtocol Protocol { get; set; }
        
        /// <summary>
        /// 端口类型
        /// </summary>
        public PortCategory PortCategory { get; set; }
        /// <summary>
        /// 远程IP地址
        /// </summary>
        public string RemoteIP { get; set; }
        /// <summary>
        /// 远程端口地址
        /// </summary>
        public int RemotePort { get; set; }
        /// <summary>
        /// 本地IP地址
        /// </summary>
        public string LocalIP { get; set; }
        /// <summary>
        /// 本地端口地址
        /// </summary>
        public int LocalPort { get; set; }
        /// <summary>
        /// 串口号
        /// </summary>
        public string Comx { get; set; }
        /// <summary>
        /// 波特率
        /// </summary>
        public int BaudRate { get; set; }
        /// <summary>
        /// 串口参数
        /// </summary>
        public string SerialParams { get; set; }
        /// <summary>
        /// 站地址
        /// </summary>
        public int Station { get; set; }
        /// <summary>
        /// 只读寄存器列表
        /// </summary>
        public string RoList { get; set; }
        /// <summary>
        /// 读写寄存器列表
        /// </summary>
        public string RwList { get; set; }
        /// <summary>
        /// udp方式下的通信帧
        /// </summary>
        public string ProtocolDefine { get; set; }
        /// <summary>
        /// udp方式下间隔时间ms
        /// </summary>
        public int Cycle { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public SlaveDto()
        {
            Name = "UserStation01";
            Description = "用户自定义服务器";
            Protocol = SlaveProtocol.Modbus_RTU;
            PortCategory = PortCategory.TCP;
            RemoteIP = "127.0.0.1";
            RemotePort = 6000;
            LocalIP = "127.0.0.1";
            LocalPort = 7000;
            Comx = "COM1";
            BaudRate = 9600;
            SerialParams = "N-8-1";
            Station = 1;
            RoList = "";
            RwList = "";
            ProtocolDefine = "";
            Cycle = 100;
        }

        /// <summary>
        /// 拷贝函数
        /// </summary>
        /// <param name="other"></param>
        public static SlaveDto Clone(SlaveDto other)
        {
            var current = new SlaveDto(){
                Id = other.Id,
                Name = other.Name,
                Description = other.Description,
                Protocol = other.Protocol,
                PortCategory = other.PortCategory,
                RemoteIP = other.RemoteIP,
                RemotePort = other.RemotePort,
                LocalIP = other.LocalIP,
                LocalPort = other.LocalPort,
                Comx = other.Comx,
                BaudRate = other.BaudRate,
                SerialParams = other.SerialParams,
                Station = other.Station,
                RoList = other.RoList,
                RwList = other.RwList,
                ProtocolDefine = other.ProtocolDefine,
                Cycle = other.Cycle
            };
            return current;
    }
}